#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;
class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        vector<int>dp(arr.size(),1);
        //优化 n^2 ->n
        unordered_map<int,int>u_map;
        u_map[arr[0]]=0;
        for(int i=1;i<arr.size();i++){
            if(u_map.count(arr[i]-difference))
                dp[i]=max(dp[i],dp[u_map[arr[i]-difference]]+1);
            u_map[arr[i]]=i;
        }
        return *max_element(dp.begin(),dp.end());
    }
};